layui.use(['form', 'jquery'], function () {
        var form = layui.form;
        var $ = layui.jquery;
        var layer = layui.layer;
        form.on('submit(register)', function (res) {
            var requestData = res.field
            if (requestData.password !== requestData.re_password) {
                layer.msg("两次密码不一致", {icon: 2, time: 1000})
            } else {
                $.ajax({
                    url: 'http://127.0.0.1:8000/user/user/register/',
                    type: 'POST',
                    data: {
                        "username": requestData.username,
                        "email": requestData.email,
                        "password": requestData.password,
                        "re_password": requestData.re_password
                    },
                    success: function (res) {
                        if (res.status === 400) {
                            layer.msg(res.message, {icon: 5, time: 1000})
                        } else if (res.status === 201) {
                            //注册成功后清空表单内容
                            $('input[name="username"]').val('');
                            $('input[name="email"]').val('');
                            $('input[name="password"]').val('');
                            $('input[name="re_password"]').val('');
                            layer.msg(res.message, {icon: 1, time: 2000})
                        }
                    },
                    error: function (res) {
                        layer.msg(res.message, {icon: 2, time: 1000})
                    }
                })
            }
            return false; //阻止表单跳转
        });
    });